﻿using Telerik.Web.UI;
using Guru.Utilities;
using System;
using Guru.Web.Controls.Asp;
using System.Web.UI.WebControls;
using Guru.Web.UI;

namespace Guru.Web.Controls.Telerik
{
    public class GuruGridBoundColumn : GridBoundColumn
    {       
        public GridFormat Format
        {
            set;
            get;
        }

        public GuruGridBoundColumn() : base()
        {
            this.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            this.HeaderStyle.Font.Bold = true;
            this.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        }
        
        public override string SortExpression
        {
            get
            {
                if (string.IsNullOrEmpty(base.SortExpression))
                    return base.DataField;
                return base.SortExpression;
            }
            set
            {
                base.SortExpression = value;
            }
        }
     
        protected override string FormatDataValue(object dataValue, GridItem item)
        {          
            switch (this.Format)
            {
                case GridFormat.None:
                    this.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
                    break;
                case GridFormat.Currency:
                    this.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
                    string lang = ((GuruPage)this.Owner.Page).CurrentLanguage;
                    return Formatter.FormatCurrency(dataValue, lang);                   
                case GridFormat.Date:
                    this.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    return Formatter.FormatDate(dataValue);                  
                case GridFormat.DateTime:
                    this.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    break;
                case GridFormat.Number:
                    this.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
                    break;
                case GridFormat.Phone:
                    this.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    return dataValue.FormatPhone();                   
                case GridFormat.StaticString:
                    this.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    break;
                case GridFormat.Sequence:
                    this.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                    this.ItemStyle.Width = this.HeaderStyle.Width = new Unit(30, UnitType.Pixel);
                    this.AllowFiltering = false;
                    this.AllowSorting = false;
                    int rowNumber = this.Owner.OwnerGrid.PageSize * this.Owner.OwnerGrid.CurrentPageIndex + item.ItemIndex + 1;
                    return rowNumber.ToString();                   
            }
            return base.FormatDataValue(dataValue, item);
        }
        
    }

    public class GuruGridTemplateColumn : GridTemplateColumn
    {
        public GuruGridTemplateColumn()
            : base()
        {
            this.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            this.HeaderStyle.Font.Bold = true;
            this.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        }
    }
    public class GuruGridClientSelectColumn : GridTemplateColumn
    {
        public const string ItemCheckBoxID = "uxItemCheckBox";
        public const string HeaderCheckBoxID = "uxHeaderCheckBox";
        public GuruGridClientSelectColumn()
            : base()
        {
            this.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;           
            this.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        }
        
        public override void Initialize()
        {
            base.Initialize();
           
            this.Owner.OwnerGrid.ItemDataBound += new GridItemEventHandler(DoGridItemDataBound);
            this.Owner.OwnerGrid.PreRender += new EventHandler(DoPreRender);
        } 
        public override void InitializeCell(System.Web.UI.WebControls.TableCell cell, int columnIndex, GridItem inItem)
        {
            base.InitializeCell(cell, columnIndex, inItem);
            switch (inItem.ItemType)
            {
                case GridItemType.Item:
                case GridItemType.AlternatingItem:
                    Guru.Web.Controls.Asp.CheckBox itemCheckBox = new Guru.Web.Controls.Asp.CheckBox();
                    itemCheckBox.ID = "uxItemCheckBox";
                    itemCheckBox.CssClass = "ItemCheckBox";
                    cell.Controls.Add(itemCheckBox);
                    break;
                case GridItemType.Header:
                    Guru.Web.Controls.Asp.CheckBox headerCheckBox = new Guru.Web.Controls.Asp.CheckBox();
                    headerCheckBox.ID = "uxHeaderCheckBox";
                    headerCheckBox.CssClass = "HeaderCheckBox";
                    cell.Controls.Add(headerCheckBox);                    
                    break;
            }
        }
        protected void DoGridItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridHeaderItem)
            {
                Guru.Web.Controls.Asp.CheckBox headerCheckBox = e.Item.FindControl("uxHeaderCheckBox") as Guru.Web.Controls.Asp.CheckBox;
                headerCheckBox.Attributes.Add("onclick", string.Format("GuruGridClientSelectColumn_HeaderClick(this, '{0}','ItemCheckBox')", this.OwnerGridID));       
            }            
        }              
        protected void DoPreRender(object sender, EventArgs e)
        {
            this.AllowFiltering = false;
            this.SortExpression = string.Empty;

            //Regist javascript
            string clientScript = this.Owner.Page.ClientScript.GetWebResourceUrl(typeof(GuruGridClientSelectColumn), "Guru.Web.Controls.EmbeddedResource.GridSelectedColumn.js");
            this.Owner.Page.ClientScript.RegisterClientScriptInclude("Guru.Web.Controls.GridSelectedColumn.js", clientScript);

        }
    }
}
